using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {
	
	public Vector3 moveDirection;
	public float moveSpeed = 0.0f;
	public float verticalSpeed = 10.0f;
	public float rotateSpeed = 150.0f;
	public float speedSmoothing = 100.0f;
	public float speedFactor = 10.0f;
	public float gravity = 5.0f;
	public float jumpingFactor = 15f;
	public CollisionFlags collisionFlags; 
	private Vector3 inAirVelocity = Vector3.zero;	
	public float timeLastJump = 0.0f;
	public int timeButtonCount = 0;
	public float delayBetweenSecondJump = 1.0f;
	public float timeJump = 2.0f;

	
	// Use this for initialization
	void Start () 
	{
		//moveDirection = transform.TransformDirection(Vector3.forward);
	}
	
	// Update is called once per frame
	void Update () 
	{
		updateMoveDirection();
		applyGravity();
		applyJumping();
		// Calculate actual motion
		Vector3 verticalVec = new Vector3(0,verticalSpeed,0);	
		
		
		Debug.DrawRay(transform.position,moveDirection * 10,Color.blue);
		Debug.DrawRay(transform.position,verticalVec,Color.black);
			
		Vector3 movement = moveDirection * moveSpeed + verticalVec + inAirVelocity ;
		movement *= Time.deltaTime;
	
		// Move the controller
		CharacterController controller = (CharacterController)GetComponent("CharacterController");
		collisionFlags = controller.Move(movement);
	
		// Set rotation to the move direction
		if (isMoving())
		{
			transform.rotation = Quaternion.LookRotation(moveDirection);
		}	
	}
	
	void updateMoveDirection()
	{
		//build the sixaxis direction vector.
		Transform cameraTransform = Camera.main.transform;
			
		float x = Input.GetAxisRaw("Horizontal");
		float y = Input.GetAxisRaw("Vertical");
			
			
			
		// our targetDirection vector
		Vector3 targetDirection = new Vector3(x,0,y);
		targetDirection = cameraTransform.TransformDirection(targetDirection);
		targetDirection.y = 0;
		//targetDirection = targetDirection.normalized;
			
		Debug.DrawRay(transform.position,targetDirection * 10,Color.red);
		moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
		moveDirection = moveDirection.normalized;
			
		// Smooth the speed based on the current target direction
		var curSmooth = speedSmoothing * Time.deltaTime;
			
		// Choose target speed
		//* We want to support analog input but make sure you cant walk faster diagonally than just forward or sideways
		var targetSpeed = Mathf.Min(targetDirection.magnitude, 1.0f);
		moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth) * speedFactor;
	}
	
	public bool isGrounded() 
	{
		return (collisionFlags & CollisionFlags.CollidedBelow) != 0;
	}
	
	public bool isMoving()
	{
		return Mathf.Abs(Input.GetAxisRaw("Vertical")) + Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0.1;
	}
	
	public void applyGravity()
	{
		if (isGrounded ())
			verticalSpeed = 0.0f;
		else
			verticalSpeed -= gravity * Time.deltaTime;
	}
	
	public void applyJumping()
	{
		if(Input.GetButtonDown("Triangle"))
		{
			if(timeButtonCount == 0)
			{
				// first jump
				timeButtonCount++;
				verticalSpeed += jumpingFactor;
			}
			else if(timeButtonCount == 1 && verticalSpeed >= 0)
			{
				// second jump	
				timeButtonCount++;
				verticalSpeed += jumpingFactor;
			}
		}
		if(isGrounded())
		{
			timeButtonCount = 0;
		}
	}
}
